<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.username" placeholder="姓名"  @input="verifyText($event, 'name', 'filters')"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUser">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

    <!--列表-->
    <template>
      <el-table
        :data="users"
        highlight-current-row
        v-loading="listLoading"
        style="width: 100%"
      >
        <el-table-column type="index" width="60"> </el-table-column>
        <el-table-column prop="username" label="姓名" width="120" sortable>
        </el-table-column>
		<el-table-column prop="permission" label="权限" width="120" sortable>
        </el-table-column>
        <el-table-column
          prop="sex"
          label="性别"
          width="100"
          :formatter="formatSex"
          sortable
        >
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="100" sortable>
        </el-table-column>
        <el-table-column prop="birth" label="生日" width="120" sortable>
        </el-table-column>
        <el-table-column prop="addr" label="地址" min-width="180" sortable>
        </el-table-column>
      </el-table>
    </template>
  </section>
</template>
<script>
import util from "../../common/js/util";
//import NProgress from 'nprogress'
import {
  getDepartmentListPage,
  removeDepartment,
  batchRemoveDepartment,
  editDepartment,
  addDepartment,
} from "../../api/api";
//import NProgress from 'nprogress'
export default {
  data() {
    return {
      filters: {
        username: "",
      },
      options: ["未知", "已租", "等待出租"], //给下拉框查询用的
      catelist: [], //用来装车辆类型的
      users: [], //装列表数据的
      total: 55,
      pageSizes: [5, 10, 15, 20], //分页的规则
      currentPage: 1, //当前页
      pageSize: 10, //每页显示条目个数
      title: "",
      listLoading: false,
      sels: [], //列表选中列
      //新增和修改公用同一套
      editFormVisible: false, //编辑界面是否显示
      editLoading: false, //忙等框
      editFormRules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
      },
      //编辑界面数据 准备一个对象用于给后台传递数据 和模型层里面的条条框框绑定
      editForm: {
        id: null,
        username: "",
        password: "",
        permission: null,
        user_category: "",
      },
    };
  },
  methods: {
    search() {
      //查询还是交给查询全部去做，但是为了排除一个bug需要跳转到第一页
      this.currentPage = 1;
      //然后调用查询方法
      this.getusers();
    },

    //六、提交按钮执行添加
    editSubmit() {
      //1、在这里可以实现一个校验表单的过程
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          //如果valid为true，就是说校验表单的东西都完成了
          //提示文案
          this.$confirm("确认提交吗？", "温馨提示", { type: "warning" })
            .then(() => {
              //2、开启忙等框
              this.editLoading = true;
              //3、获取到所有的数据
              let para = Object.assign({}, this.editForm);
              //4、发送请求调用接口
              this.$http
                .put("/user", para)
                .then((result) => {
                  console.log(result.data);
                  //关闭忙等框
                  this.editLoading = false;
                  //刷新页面，且重新加载
                  location.reload();
                  this.getUsers();
                })
                .catch((result) => {
                  console.log(result.data);
                  //关闭忙等框
                  this.editLoading = false;
                  //刷新页面，
                  location.reload();
                });
            })
            .catch(() => {
              console.log("取消提交");
            });
        }
      });
    },

    //四、删除单条的方法
    handleDel(index, row) {
      this.$confirm("是否删除该数据？", "温馨提示", { type: "warning" })
        .then(
          //再设置一个函数，成功了就执行
          () => {
            //获取到id
            let id = row.id;
            //发送axios请求
            this.$http
              .delete("/user/" + id)
              .then((result) => {
                //如果成功了，就打印信息并且重新加载页面
                console.log(result);
                this.getUsers();
              })
              .catch((result) => {
                console.log(result);
              });
          }
        )
        .catch((result) => {
          console.log("取消删除操作");
        });
    },

    //三、页码发生改变
    handleCurrentChange(val) {
      console.log(val);
      /*把新页码交给模型层的数据,页码发生改变，1，把页码赋值给当前页码，带着一页多少条一起去数据库查*/
      this.currentPage = val;
      console.log(
        "赋值后的当前页：" + this.currentPage + "页总数：" + this.pageSize
      );
      //2、调用getusers查询
      this.getUsers();
    },
    //二、页数发生改变
    handleSizeChange(val) {
      /*分析，页数量发生改变，把数量赋值给模型层*/
      console.log(val);
      console.log(
        "赋值后的当前页：" + this.currentPage + "页总数：" + this.pageSize
      );
      this.pageSize = val;
      //调用depart方法
      this.getUsers();
    },

    //显示编辑界面
    handleEdit: function (index, row) {
      //给title赋值
      this.title = "编辑";
      this.editFormVisible = true;
      //把当前行的赋值给表单里面 克隆，不克隆有bug
      this.editForm = Object.assign({}, row);
    },
    //显示表单，进行添加
    handleAdd() {
      //给title赋值
      this.title = "新增";
      this.editFormVisible = true; //新增页面打开表单
      //重置表单，如果不重置的话，会很麻烦
      this.editForm = {
        id: null,
        address: "",
        age: null,
        username: "",
        phone: "",
        identity: "",
        user_category: "",
      };
      console.log(this.editForm);
    },
    selsChange: function (sels) {
      this.sels = sels;
    },

    //性别显示转换
    formatSex: function (row, column) {
      return row.sex == 1 ? "男" : row.sex == 0 ? "女" : "未知";
    },
    //获取用户列表
    // getUser: function () {
    //   let para = {
    //     username: this.filters.username,
    //   };
    //   this.loading = true;
    //   //NProgress.start();
    //   getUserList(para).then((res) => {
    //     this.users = res.data.users;
    //     this.loading = false;
    //     //NProgress.done();
    //   });
    // },
    getUsers() {
      console.log(this.filters.keyWord);
      //1、将数值加到对象里面
      let para = {
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        keyWord: this.filters.keyWord,
        status: this.filters.status,
        user_category: this.filters.user_category,
      };
      // console.log(para);
      //2、简单写法，显示到列表
      this.$http
        .post("/user", para)
        .then((result) => {
          // console.log(result.data);
          //3、把返回的数值同步到模型层
          this.users = result.data.rows;
          this.total = result.data.total;
        })
        .catch((result) => {
          // console.log("查询失败，请刷新重试");
        });
    },
  },
  mounted() {
    this.getUsers();
  },
};
</script>

<style scoped>
</style>